package org.easy.core.util;

import java.util.*;


public class MapUtil {

    /**
     * 根据keys批量读取Map
     *
     * @param map
     * @param keys
     * @param <K>
     * @param <V>
     * @return
     */
    public static <K, V> List<V> batchGet(Map<K, V> map, Collection<K> keys) {
        List<V> list = new ArrayList<>();
        for (K key : keys) {
            V e = map.get(key);
            if (e == null) {
                continue;
            }
            list.add(e);
        }
        return list;
    }


    /**
     * 分组id关系转分组id对象
     *
     * @param kSubKMap
     * @param idModularMap
     * @param <K>
     * @param <SubV>
     * @param <SubK>
     * @return
     */
    public static <K, SubV, SubK> Map<K, List<SubV>> relationIdToMap(Map<K, Set<SubK>> kSubKMap, Map<SubK, SubV> idModularMap) {
        Map<K, List<SubV>> resultMap = new HashMap<>();
        kSubKMap.forEach((firstKey, keys) -> {
            List<SubV> value = MapUtil.batchGet(idModularMap, keys);
            if (value != null && value.size() > 0) {
                resultMap.put(firstKey, value);
            }
        });
        return resultMap;
    }
}
